package rs.daolao.syc.admin.account.config;/*
 *   @author:Casin
 *   @date:2023/8/8
 *   @code:
 */
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

@Configuration
public class RedisConfiguration {

    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        /**方法参数上声明一个连接工厂 依赖配置了在Spring容器中 不用写配置*/

        /**创建了一个名为redisTemplate的RedisTemplate实例，指定了RedisTemplate的泛型参数*/
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        /**连接工厂*/
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        /**序列化 Serializer:序列化器*/
        redisTemplate.setKeySerializer(RedisSerializer.string());//字符串的序列化器
        redisTemplate.setValueSerializer(RedisSerializer.json());//json的序列化器

        redisTemplate.setHashKeySerializer(RedisSerializer.string());//将哈希键作为字符串进行序列化和反序列化。
        redisTemplate.setHashValueSerializer(RedisSerializer.json());//将哈希值作为 JSON 格式进行序列化和反序列化

        return redisTemplate;
    }

}
